package com.tsd.system.controller;

import com.tsd.common.controller.BaseController;
import com.tsd.core.utils.HlpUtils;
import com.tsd.system.SysConstants;
import com.tsd.system.entity.SysResourceExt;
import com.tsd.system.entity.SysUserExt;
import com.tsd.system.entity.SysUserRoleExt;
import com.tsd.system.service.SysUserRoleService;
import com.tsd.system.service.SysUserService;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WcdBaseController
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2019/7/31 14:13
 * @Version: 1.0
 */
public class SystemBaseController extends BaseController {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysUserRoleService sysUserRoleService;

    /**
     * 检查当前登录用户是否绑定员工
     *
     * @throws Exception
     */
    public void checkBindGroupPerson() throws Exception {
        //TODO 是否需要绑定员工,可以通过配置进行
//        SysUserExt opUser = super.getSysUser();
//        if (HlpUtils.isEmpty(opUser.getGroup_person_sid())) {
//            SysUserExt userExt = sysUserService.queryBySid(opUser.getSid());
//            if (HlpUtils.isEmpty(userExt.getGroup_person_sid())) {
//                throw new HlpException("当前登录用户未绑定员工");
//            }
//        }
    }

    /**
     * 添加数据权限
     *
     * @param params
     * @throws Exception
     */
    public void addDataPermission(Map<String, Object> params) throws Exception {
        SysUserExt opUser = super.getSysUser();
        SysUserExt userExt = sysUserService.queryBySid(opUser.getSid());
        super.checkEmpty(userExt, "用户不存在");
//        if (HlpUtils.isEmpty(userExt.getIs_all_client()) || userExt.getIs_all_client() == 0) {
//            params.put("data_permission", opUser.getSid());
//        }
    }

    /**
     * 判断是否有高级审核权限
     *
     * @return
     * @throws Exception
     */
    public boolean checkAdvancedAuditPermission() throws Exception {
        SysUserExt opUser = super.getSysUser();
        List<SysUserRoleExt> userRoleList = sysUserRoleService.findByUserId(opUser.getSid());
        //TODO 需要根据动态的权限进行设置
        return !HlpUtils.isEmptyList(userRoleList);//ListUtil.anyMatch(userRoleList, t -> !HlpUtils.isEmpty(t.getAdvanced_audit()) && t.getAdvanced_audit() == 1);
    }

    /**
     * 判断是否有权限
     *
     * @return
     * @throws Exception
     */
    public boolean checkPermission(String userSid, List<String> permissionList) throws Exception {
        List<SysResourceExt> list = sysUserService.findUserPermission(userSid, permissionList);
        return !HlpUtils.isEmptyList(list);
    }
}
